function SearchController() {}
SearchController.prototype.params = {};

SearchController.prototype.freeMemory = function(isSearchPage, isPageHome) {
    if (isSearchPage) {
        $('#wordItemSch').val('');
        $('#priceItemSch').val('1').selectmenu('refresh');
        $('#fromPrice').val('1');
        $('#toPrice').val('');
        $('#cardItemSch').attr("checked",false).checkboxradio("refresh");
        $('#postageItemSch').attr("checked",false).checkboxradio("refresh");
    } else {
        $('#schBookmarkList').html('').listview('refresh');
        $('#schItemList').html('').listview('refresh');
        SearchService.prototype.options = null;
    }
    
    if (isPageHome) {
        SearchController.prototype.params = null;
    }
};

$(function(){
     $(document).on('pageshow', '#pageItemSearch', function(){
        PageChangeController.prototype.freeMemory();
        monaca.updateUIStyle("titlelable", "text", "フリーワード検索");
        monaca.updateUIStyle("backheader", "visibility", "true");
        monaca.updateUIStyle("footer", "visibility", "false");
        
        Util.prototype.changeSnapperElement("itemsearch-content");
        
        Util.prototype.hideSpinner();
    });
    
    $(document).on('pageshow', '#pageItemSearchResult', function(){
        PageChangeController.prototype.freeMemory();
        monaca.updateUIStyle("titlelable", "text", "フリーワード検索結果");
        monaca.updateUIStyle("footer", "visibility", "false");
        
        Util.prototype.changeSnapperElement("itemsearchresult-content");

        if (SearchService.prototype.curPage < 0) {
            $('.itemsearch-pull').hide();    
        } else {
            $('.itemsearch-pull').show();
        }
        
        var schBookmarkList = $('#schBookmarkList'); 
        
        $('#navSchItem').addClass($.mobile.activeBtnClass);
        $('#navSchBookmark').removeClass($.mobile.activeBtnClass);
        $('#schItemList').show();
        schBookmarkList.hide();
        
        SearchService.prototype.searchLoading = false;
        SearchService.prototype.bookmarkLoading = false;
                
        SearchService.prototype.showItemList(SearchController.prototype.params, 1, function() {
            SearchService.prototype.searchLoading = true;
            Util.prototype.refreshPageScroller();
            if (SearchService.prototype.bookmarkLoading) {
                Util.prototype.hideSpinner();
            }
        });
        
        // 関連ブックマーク一覧作成 → 非表示
        ProductListLoader.prototype.initLoadRelativeItemSearch(SearchController.prototype.params.keyword);
        ProductListLoader.prototype.loadProducts(schBookmarkList, $(".itemsearch-pull"), function(){
            SearchService.prototype.bookmarkLoading = true;
            if (SearchService.prototype.searchLoading) {
                Util.prototype.hideSpinner();
            }
        }, "該当する商品が見つかりません");
    });
    
    // テキスト入力中にEnterキー押下 → 次のテキストにフォーカス移動
   $("#fromPrice").keypress(function(e) {
       var c = e.which ? e.which : e.keyCode;
       console.log(c);
       if (c == 13) { 
           $("#toPrice").focus();
           $("#toPrice").trigger('click');
           e.preventDefault();
       } 
   });
    
    // 商品一覧タブタップ
    $(document).on('tap', '#navSchItem', function(){
        $('#schItemList').show();
        $('#schBookmarkList').hide();
        if (SearchService.prototype.searchPullShow) {
            $('.itemsearch-pull').show(); 
        } else {
            $('.itemsearch-pull').hide(); 
        }
        Util.prototype.refreshPageScroller();
        return false;
    });
    
    // 関連ブックマークタブタップ
    $(document).on('tap', '#navSchBookmark', function(){
        $('#schBookmarkList').show();
        $('#schItemList').hide();
        if (SearchService.prototype.bookmarkPullShow) {
            $('.itemsearch-pull').show(); 
        } else {
            $('.itemsearch-pull').hide(); 
        }
        Util.prototype.refreshPageScroller();
        return false;
    });
  
    $(document).on('tap', '#itemRsttBtn', function(){
        SearchController.prototype.freeMemory(true, false);
    });
  
    // 検索ボタンタップ
    $(document).on('tap', '#itemSchBtn', function(){        
        var keyword = $('#wordItemSch').val();
        var fromPrice = Util.prototype.removeComma($('#fromPrice').val());//from価格
        var toPrice = Util.prototype.removeComma($('#toPrice').val());//to価格
        
        if (keyword.length < 1) {
            alert("フリーワードは1文字以上を必ず入力してください");
        
        }else if ((Util.prototype.checkNumber(fromPrice) == "false" || Util.prototype.checkNumber(toPrice) == "false")) {
            alert("価格には1～999,999,999の整数を入力してください");
            
        }else if ((parseInt(fromPrice, 10)) > (parseInt(toPrice, 10))) {
            alert("最低価格より最高価格を高くしてください");
        
        }else {
            Util.prototype.showSpinner();            
            if ($('#cardItemSch').is(':checked')) {
                $('#cardItemSch').val('1');
            }
            if ($('#postageItemSch').is(':checked')) {
                $('#postageItemSch').val('1');
            }
            var params = { keyword: keyword,
                            fromPrice: fromPrice,
                            toPrice: toPrice,
                            cardItemSch: $('#cardItemSch').val(),
                            postageItemSch: $('#postageItemSch').val()
                        };

            PageChangeController.prototype.changeToPage('pageItemSearchResult', params);
        }
    });
   
   // 価格帯を選択→fromPrice、toPriceに反映させる
    $('#priceItemSch').change(function(){
        $('#fromPrice').val($(this).val());
        $('#toPrice').val('');
        return true;
    });
   
    // fromPrice、toPriceからフォーカスが外れたとき
    $('#fromPrice').blur(function(){
        var price = Util.prototype.getHalfChar(this.value);
            price = Util.prototype.removeComma(price);
            price = Util.prototype.addComma(price);
        $(this).val(price);
    });
   
    $('#toPrice').blur(function(){
       var price = Util.prototype.getHalfChar(this.value);
           price = Util.prototype.removeComma(price);
           price = Util.prototype.addComma(price);
        $(this).val(price);
    });
    
    // fromPrice、toPriceにフォーカスが当たったとき
    $('#fromPrice').focus(function(){
        var price = Util.prototype.removeComma(this.value);
        $(this).val(price);
    });
   
    $('#toPrice').focus(function(){
       var price = Util.prototype.removeComma(this.value);
       $(this).val(price);
    });  
    
    $(document).on('tap', 'table.searchTable .shopDetailLink', function() {
        var tableParent = $(this).parents("table.searchTable");
        PageChangeController.prototype.changeToPage('pageItemList', {'ecsite_code': tableParent.attr('ecSiteCode'), 'item_id': tableParent.attr('itemCode')});
    });
    
    $(document).on("tap", "table.searchTable td.bm_star", function() {
        var tableParent = $(this).parents("table.searchTable");
        var starElement = $("table.searchTable td.bm_star div.item_head span.star_checkbox");
        var thisStarElement = $(this).find(starElement);  
        thisStarElement.toggleClass("star_clicked");
        var id = thisStarElement.attr("id");
        var bmId =  thisStarElement.attr("bmId");
        var isBookmarked = (typeof bmId != "undefined" && bmId != null && bmId != "");

        if (isBookmarked) {
            Util.prototype.showSpinner('削除しています');
            BookmarksService.prototype.deleteAdvanceBookmark([bmId], null, function() {
                $("#" + id).removeAttr('bmId');
                Util.prototype.hideSpinner();
            });
        } else {
            Util.prototype.showSpinner('ブックマークしています');
            var param = {};
            var today = Util.prototype.getTodayObj();
            param.registered_date = today.ymd;
            param.registered_month = today.m;
            param.registered_year = today.y;                    
            param.status = 1;
            param.item_name = tableParent.attr('itemName');
            param.ecsite_code = tableParent.attr('ecSiteCode');
            param.item_id = tableParent.attr('itemCode');
            param.image_url = tableParent.attr('image');
            
            param.saveLogType = DetailProductService.prototype.SERVER_LOG_BOOKMARK;
            
            DetailProductService.prototype.getGenreByGenreIdOriginal(param.ecsite_code, tableParent.attr('genreIdOriginal'), function(genreData) {
                param.genre_code = genreData.code; 
                BookmarksService.prototype.saveScanBookmark('', param, function(savedId) {                    
                    $("#" + id).attr('bmId',savedId);
                    Util.prototype.hideSpinner();
                }, function() {
                    thisStarElement.toggleClass("star_clicked");
                    Util.prototype.hideSpinner();
                });
            },function(Ex) {
                param.genre_code = 14;
                BookmarksService.prototype.saveScanBookmark('', param, function(savedId) {                    
                    $("#" + id).attr('bmId',savedId);
                    Util.prototype.hideSpinner();
                }, function() {
                    thisStarElement.toggleClass("star_clicked");
                    Util.prototype.hideSpinner();
                });
            });
        }
    });
});
